package threadedSimulator;

/**
 * The system timer is an autonomous device that 
 * has a standard time delay, after which it generates an interrupt. 
 * These interrupts are used to trigger the scheduler, thereby 
 * supporting preemptive multitasking. For purposes of this 
 * simulation, you can set all process quanta to the same value. 
 * @author Andres Rosales (acr)
 * @version 3/13/2014
 */
public class Timer extends javax.swing.Timer {
	/**
	 * The integer representation of the amount of quanta each process will be allowed.
	 */
	private static final int QUANTA = 1000;
	
	/**
	 * The Cpu associated with this Timer.
	 */
	private Cpu my_cpu;
	
	/**
	 * The boolean representing the pause-state of the Timer.
	 */
	private boolean is_paused;
	
	/**
	 * Constructs and initializes a new instance of the Timer class.
	 */
	public Timer(Cpu the_cpu) {
		super(QUANTA, null);
		my_cpu = the_cpu;
		this.addActionListener(my_cpu);
	}
	
	/**
	 * Constructs and initializes a new instance of the Timer class.
	 */
	public Timer(Cpu the_cpu, int intitial_delay) {
		super(QUANTA, null);
		setInitialDelay(intitial_delay);
		my_cpu = the_cpu;
		this.addActionListener(my_cpu);
	}
	
	public void start() {
		super.start();
		is_paused = false;
	}
	
	public void stop() {
		super.stop();
		is_paused = true;
	}
	
	public boolean isPaused() {
		return is_paused;
	}
	
	/**
	 * Returns the name of this object.
	 * 
	 * @return The name of this object.
	 */
	public String getName() {
		return "Timer";
	}
}
